package com.wenbai.servicelightluxurystickers.controller;

import com.wenbai.servicelightluxurystickers.entity.StickUser;
import com.wenbai.servicelightluxurystickers.service.StickUserService;
import com.wenbai.servicelightluxurystickers.utils.Response;
import com.wenbai.servicelightluxurystickers.utils.TokenUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stick/user")
public class StickUserController {

    @Autowired
    private StickUserService stickUserService;

    @Data
    public static class LoginRequest {
        private String account;
        private String password;
    }

    @PostMapping("/login")
    public Response<Map<String, Object>> login(@RequestBody final LoginRequest loginRequest) {
        StickUser user = stickUserService.login(loginRequest.account, loginRequest.password);
        if (user != null) {
            String token = TokenUtils.token(user.getAccount(), user.getPasswordHash());
            Map<String, Object> result = new HashMap<>();
            result.put("token", token);
            result.put("user", user);
            return Response.success(result, "登录成功");
        } else {
            return Response.error("账号或密码错误");
        }
    }

    @PostMapping("/register")
    public Response<String> register(@RequestBody StickUser user) {
        // 检查必填字段
        if (user.getAccount() == null || user.getPasswordHash() == null) {
            return Response.error("账号和密码不能为空");
        }
        boolean success = stickUserService.register(user);
        if (success) {
            return Response.success("注册成功");
        } else {
            return Response.error("账号已存在，注册失败");
        }
    }

    @PostMapping("/update")
    public Response<String> update(@RequestBody StickUser user) {
        boolean success = stickUserService.update(user);
        if (success) {
            return Response.success("更新成功");
        } else {
            return Response.error("更新失败");
        }
    }

    @Data
    public static class UpdatePasswordRequest {
        private String account;
        private String password;
        private String newPassword;
    }

    @PostMapping("/updatePassword")
    public Response<String> updatePassword(@RequestBody UpdatePasswordRequest updatePasswordRequest) {
        boolean success = stickUserService.updatePassword(updatePasswordRequest.account, updatePasswordRequest.password, updatePasswordRequest.newPassword);
        if (success) {
            return Response.success("密码更新成功");
        } else {
            return Response.error("密码更新失败");
        }
    }

    @GetMapping("/selectUser")
    public Response<List<StickUser>> selectUser() {
        List<StickUser> users = stickUserService.selectUser();
        return Response.success(users, "查询成功");
    }

    @PostMapping("/insertUser")
    public Response<String> insertUser(@RequestBody StickUser user) {
        boolean success = stickUserService.insertUser(user);
        if (success) {
            return Response.success("插入成功");
        } else {
            return Response.error("插入失败");
        }
    }

    @PostMapping("/deleteUser")
    public Response<String> deleteUser(@RequestBody String userId) {
        boolean success = stickUserService.deleteUser(userId);
        if (success) {
            return Response.success("删除成功");
        } else {
            return Response.error("删除失败");
        }
    }

}
